//
//  NewsViewController.m
//  NBNews
//
//  Created by Ruan on 14-10-8.
//  Copyright (c) 2014年 Hero. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsContentScrollView.h"
#import "NewsNavBarView.h"
#import "TopNewsViewController.h"
#import "TechViewController.h"
#import "MovieViewController.h"
#import "PhoneViewController.h"
#import "SportsViewController.h"
#import "GamesViewController.h"
#import "DigitalViewController.h"
#import "NewsNavBarViewDelegate.h"
#import "AFHTTPRequestOperationManager.h"
#import "RunViewController.h"
#import "FashionViewController.h"
#import "SocietyViewController.h"
#import "ConnectNet.h"

#define topNewsURLString @"http://c.3g.163.com/nc/article/list/T1348647909107/0-20.html"
#define techURLString @"http://c.m.163.com/nc/article/list/T1348649580692/0-20.html"
#define movieURLString @"http://c.3g.163.com/nc/article/list/T1348648650048/0-20.html"
#define sportURLString @"http://c.3g.163.com/nc/article/list/T1348649079062/0-20.html"
#define phoneURLString @"http://c.m.163.com/nc/article/list/T1348649654285/0-20.html"
#define gamesURLString @"http://c.3g.163.com/nc/article/list/T1397016069906/0-20.html"
#define digitalURLString @"http://c.m.163.com/nc/article/list/T1348649776727/0-20.html"
#define runURLString @"http://c.m.163.com/nc/article/list/T1411113472760/0-20.html"
#define fashionURLString  @"http://c.m.163.com/nc/article/list/T1413795322090/0-20.html"
#define societyURLString  @"http://c.m.163.com/nc/article/list/T1348654105308/0-20.html"


@interface NewsViewController () <NewsNavBarViewDelegate,UIScrollViewDelegate,TopNewsViewControllerDelegate>

@property (nonatomic, retain) NewsContentScrollView *newsContentScrollView;
@property (nonatomic, retain) NewsNavBarView *newsNavBar;
@property (nonatomic, retain) NSMutableArray *tableViewArray;

@property (nonatomic, retain) TopNewsViewController *topNewsView;
@property (nonatomic, retain) TechViewController *techView;
@property (nonatomic, retain) MovieViewController *movieView;
@property (nonatomic, retain) PhoneViewController *phoneView;
@property (nonatomic, retain) SportsViewController *sportsView;
@property (nonatomic, retain) GamesViewController *gamesView;
@property (nonatomic, retain) DigitalViewController *digitalView;
@property (nonatomic, retain) RunViewController *runView;
@property (nonatomic, retain) FashionViewController *fashionView;
@property (nonatomic, retain) SocietyViewController *societyView;

@property (nonatomic, assign) CGFloat scrollStartcontentOffsetX;

@end

@implementation NewsViewController

-(void)dealloc
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [_newsContentScrollView release];
    [_newsNavBar release];
    [_tableViewArray release];

    [_topNewsView release];
    [_techView release];
    [_movieView release];
    [_phoneView release];
    [_sportsView release];
    [_gamesView release];
    [_digitalView release];
    [_runView release];
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    if(![ConnectNet connectedToNetwork])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络连接失败,请查看网络是否连接正常！" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }else
    {
    
    
    
    
    
    
    // 添加新闻控制
    NewsNavBarView *newsNavBar = [[NewsNavBarView alloc] init];
    self.newsNavBar = newsNavBar;
    
  
    
    [self.newsNavBar.newsNavBarScorll.panGestureRecognizer addTarget:self action:@selector(newsNavBarScorllPanChange:)];
    newsNavBar.delegate = self;
    
    // 1.添加头条实例
    TopNewsViewController *topNewsView = [[[TopNewsViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    topNewsView.delegate = self;
    
    // 链接头条数据
    [topNewsView tableViewDownLoadDataForURLString:topNewsURLString andIsDowndrag:YES];

    //2 添加科技实例
    TechViewController *techView = [[[TechViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 3.添加电影实例
    MovieViewController *movieView = [[[MovieViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 4.添加电话实例
    PhoneViewController *phoneView = [[[PhoneViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 5.添加运动实例
    SportsViewController *sportsView = [[[SportsViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 6.添加游戏实例
    GamesViewController *gamesView = [[[GamesViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 7.添加数码实例
    DigitalViewController *digitalView = [[[DigitalViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];

    // 8.添加跑步实例
    RunViewController *runView = [[[RunViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 9. 添加时尚实例
    FashionViewController *fashionView = [[[FashionViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    // 10. 添加家居实例
    SocietyViewController *societyView = [[[SocietyViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];
    
    
    // 11.每个版块的属性赋值
    self.topNewsView = topNewsView;
    self.techView = techView;
    self.movieView = movieView;
    self.phoneView = phoneView;
    self.sportsView = sportsView;
    self.gamesView = gamesView;
    self.digitalView = digitalView;
    self.runView =runView;
    self.fashionView = fashionView;
    self.societyView = societyView;
    
    
    //12. 加入每个子视图
    [self addChildViewController:topNewsView];
    [self addChildViewController:techView];
    [self addChildViewController:movieView];
    [self addChildViewController:phoneView];
    [self addChildViewController:sportsView];
    [self addChildViewController:gamesView];
    [self addChildViewController:digitalView];
    [self addChildViewController:runView];
    [self addChildViewController:fashionView];
    [self addChildViewController:societyView];
    
    
    // 13.创建数组存储tableview
    NSMutableArray *tableViewArray = [[NSMutableArray alloc] init];
    self.tableViewArray = tableViewArray;
    
    [tableViewArray addObject:topNewsView.tableView];
    [tableViewArray addObject:techView.tableView];
    [tableViewArray addObject:movieView.tableView];
    [tableViewArray addObject:phoneView.tableView];
    [tableViewArray addObject:sportsView.tableView];
    [tableViewArray addObject:gamesView.tableView];
    [tableViewArray addObject:digitalView.tableView];
    [tableViewArray addObject:runView.tableView];
    [tableViewArray addObject:fashionView.tableView];
    [tableViewArray addObject:societyView.tableView];
    
    
    // 添加新闻滚动视图
    NewsContentScrollView *newsContentScrollView = [[NewsContentScrollView alloc] init];

    CGFloat tableViewX = 0;
    CGFloat tableViewY = 0;
    CGFloat tableViewW = self.view.bounds.size.width;
    CGFloat tableViewH = self.view.bounds.size.height - 64;
    
    
    // 添加滚动视图的内容
    for (int i = 0; i < tableViewArray.count; i++) {
        tableViewX = i * tableViewW;
        UITableView *table = tableViewArray[i];
        table.contentInset = UIEdgeInsetsMake(newsNavBar.bounds.size.height, 0, 0, 0);
        table.frame = CGRectMake(tableViewX, tableViewY, tableViewW, tableViewH);
        [newsContentScrollView addSubview:tableViewArray[i]];
        newsContentScrollView.contentSize = CGSizeMake((i + 1) * tableViewW, 0);
    }
    newsContentScrollView.delegate = self;
    self.newsContentScrollView = newsContentScrollView;
    
    // 通过滚动视图的手势来控制ROOTview
    [self.newsContentScrollView.panGestureRecognizer addTarget:self action:@selector(panChangeRootView:)];
    
    
    [self.view addSubview:newsContentScrollView];
    [self.view addSubview:newsNavBar];
    
    [newsContentScrollView release];
    [newsNavBar release];
    [tableViewArray release];
    
    }

}


// 通过索引控制view
- (void)newsNavBarViewChangeContentWithIndex:(NSUInteger)index
{
    [self.newsContentScrollView setContentOffset:CGPointMake(index * self.newsContentScrollView.bounds.size.width, 0) animated:NO];
    
    [self tableviewChange:(int)index];
}


// 记录滚动第一个点

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    _scrollStartcontentOffsetX = scrollView.contentOffset.x;
   
}


// 滚动结束后newsNavBar按钮也索引到对应位置
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    CGFloat scrollEndcontentOffsetX = scrollView.contentOffset.x;
    
    if (scrollEndcontentOffsetX != _scrollStartcontentOffsetX) {
        int index = self.newsContentScrollView.contentOffset.x / self.newsContentScrollView.bounds.size.width;
        [self.newsNavBar newsNavBarbuttonchangeWithIndex:index];
        
        [self tableviewChange:index];

    }
}


// 根据索引去找到对应的ROOTvIEW按钮
-(void)tableviewChange:(int)index

{
    switch (index) {
        case 0:
            [self.topNewsView tableViewDownLoadDataForURLString:topNewsURLString andIsDowndrag:YES];
            
            break;
        case 1:
            [self.techView tableViewDownLoadDataForURLString:techURLString andIsDowndrag:YES];
            break;
        case 2:
            [self.movieView tableViewDownLoadDataForURLString:movieURLString andIsDowndrag:YES];
            break;
        case 3:
            [self.phoneView tableViewDownLoadDataForURLString:phoneURLString andIsDowndrag:YES];
            
            break;
        case 4:
            [self.sportsView tableViewDownLoadDataForURLString:sportURLString andIsDowndrag:YES];
            
            break;
        case 5:
            [self.gamesView tableViewDownLoadDataForURLString:gamesURLString andIsDowndrag:YES];
            break;
            break;
        case 6:
            [self.digitalView tableViewDownLoadDataForURLString:digitalURLString andIsDowndrag:YES];
            break;
        case 7:
            [self.runView tableViewDownLoadDataForURLString:runURLString andIsDowndrag:YES];
            break;
        case 8:
            [self.fashionView tableViewDownLoadDataForURLString:fashionURLString andIsDowndrag:YES];
            break;
        case 9:
            [self.societyView tableViewDownLoadDataForURLString:societyURLString andIsDowndrag:YES];
            break;
        default:
            break;
    }
}


// 平移手势

- (void)panChangeRootView:(UIPanGestureRecognizer *)pan
{
    
     CGFloat rootViewX = [pan translationInView:pan.view].x;
     if (self.newsContentScrollView.contentOffset.x == 0) {
         
         if (pan.state != UIGestureRecognizerStateEnded) {
             self.newsContentScrollView.contentSize = CGSizeZero;
             
             if (_displayDelegate && [_displayDelegate respondsToSelector:@selector(newsViewControllerDisplayPanChangePointX:)]) {
                 [_displayDelegate newsViewControllerDisplayPanChangePointX:rootViewX];
             }
             
             [pan setTranslation:CGPointZero inView:pan.view];
         }else
         {
             self.newsContentScrollView.contentSize = CGSizeMake(self.tableViewArray.count * self.topNewsView.tableView.bounds.size.width , 0);
             
             if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerDisplayPanStateEnd)]) {
                 [_displayDelegate newsViewControllerDisplayPanStateEnd];
             }
         }
         
     }else
     {
         
         if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerCancelSwipeGesture)]) {
             
             [_displayDelegate newsViewControllerCancelSwipeGesture];
         }
     }

    }


// 滚动的手势改变
- (void)newsNavBarScorllPanChange:(UIPanGestureRecognizer *)pan
{
    if (pan.state != UIGestureRecognizerStateEnded) {
        if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerCancelSwipeGesture)]) {
            [_displayDelegate newsViewControllerCancelSwipeGesture];
        }
    }else
    {
        if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerOpenSwipeGesture)]) {
            [_displayDelegate newsViewControllerOpenSwipeGesture];
        }

    }
   
}

//顶部的取消手势
 - (void)topNewsViewControllerCancelSwipeGesture
{
    if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerCancelSwipeGesture)]) {
        [_displayDelegate newsViewControllerCancelSwipeGesture];
    }

}
//顶部的开始手势

- (void)topNewsViewControllerOpenSwipeGesture
{
    if (_displayDelegate &&[_displayDelegate respondsToSelector:@selector(newsViewControllerOpenSwipeGesture)]) {
        [_displayDelegate newsViewControllerCancelSwipeGesture];
    }
}



       




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
